#!/bin/bash

function create_summary {
# Fetch results from SQL Server
groovy -cp .:../Libraries/MSJDBC/sqljdbc4.jar --encoding utf-8 BenchmarkResults.groovy
}

function conv_imgs {
# Convert images to pdf format
echo "Converting images..."
./dia2eps.sh
./plotresults.sh
./eps2pdf.sh
}

function csv2plot {
groovy --encoding utf-8 CSV2Plot.groovy
}

function pdfplot2tex {
groovy --encoding utf-8 Plot2Tex.groovy
}


function tex2pdf {
# Create PDF file from the *.tex files
echo "Creating PDF file..."
pdflatex "$texfile"  #> /dev/null
bibtex "$texfile"".aux" > /dev/null
makeglossaries "$texfile" > /dev/null
pdflatex "$texfile" > /dev/null
pdflatex "$texfile" > /dev/null
#mv dissertation.pdf WilsonSantos_36228-dissertation.pdf
}

texfile="dissertation"

if [ $# -eq 1 ]; then
  # Clean the temporary files
  if [ $1 == "-c" ]; then
    echo "Cleaning temporary files..."
    ./clean.sh
    exit 0
  fi
  # Run only pdflatex
  if [ $1 == "-l" ]; then
    tex2pdf
    exit 0
  fi
  if [ $1 == "-i" ]; then
    conv_imgs
    tex2pdf
    exit 0
  fi
  if [ $1 == "-S" ]; then
    create_summary
    tex2pdf
    exit 0
  fi
fi

# Run all tasks
#create_summary
csv2plot
./dia2eps.sh
./plot2eps.sh
./eps2pdf.sh
pdfplot2tex
tex2pdf
